<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>VUE2硅谷知识点 | ETHAN&#39;s Blog </title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/ethanBlogs/favicon.ico">
    <meta name="description" content=" 对 JavaScript 情有独钟 ~ ">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/ethanBlogs/assets/css/0.styles.2aa58c37.css" as="style"><link rel="preload" href="/ethanBlogs/assets/js/app.41cfd4b1.js" as="script"><link rel="preload" href="/ethanBlogs/assets/js/3.30c6e821.js" as="script"><link rel="preload" href="/ethanBlogs/assets/js/1.ff0e4a20.js" as="script"><link rel="preload" href="/ethanBlogs/assets/js/21.73137a1f.js" as="script"><link rel="prefetch" href="/ethanBlogs/assets/js/10.5818770a.js"><link rel="prefetch" href="/ethanBlogs/assets/js/11.a80cfea6.js"><link rel="prefetch" href="/ethanBlogs/assets/js/12.487e572c.js"><link rel="prefetch" href="/ethanBlogs/assets/js/13.ac8db77e.js"><link rel="prefetch" href="/ethanBlogs/assets/js/14.02537dd1.js"><link rel="prefetch" href="/ethanBlogs/assets/js/15.826f7a25.js"><link rel="prefetch" href="/ethanBlogs/assets/js/16.bf53758a.js"><link rel="prefetch" href="/ethanBlogs/assets/js/17.634aa983.js"><link rel="prefetch" href="/ethanBlogs/assets/js/18.9903bbbd.js"><link rel="prefetch" href="/ethanBlogs/assets/js/19.0d127aae.js"><link rel="prefetch" href="/ethanBlogs/assets/js/20.a231df0a.js"><link rel="prefetch" href="/ethanBlogs/assets/js/22.4823be3f.js"><link rel="prefetch" href="/ethanBlogs/assets/js/4.74080337.js"><link rel="prefetch" href="/ethanBlogs/assets/js/5.f95d4eaf.js"><link rel="prefetch" href="/ethanBlogs/assets/js/6.c87f8fa0.js"><link rel="prefetch" href="/ethanBlogs/assets/js/7.fa249a34.js"><link rel="prefetch" href="/ethanBlogs/assets/js/8.53360952.js"><link rel="prefetch" href="/ethanBlogs/assets/js/9.4e81e732.js">
    <link rel="stylesheet" href="/ethanBlogs/assets/css/0.styles.2aa58c37.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1aefc0b4><div data-v-1aefc0b4><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1aefc0b4 data-v-1aefc0b4><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>ETHAN's Blog </h3> <p class="description" data-v-25ba6db2 data-v-25ba6db2> 对 JavaScript 情有独钟 ~ </p> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>ETHAN</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1aefc0b4><header class="navbar" data-v-1aefc0b4><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/ethanBlogs/" class="home-link router-link-active"><img src="/ethanBlogs/logo.png" alt="ETHAN's Blog " class="logo"> <span class="site-name">ETHAN's Blog </span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/ethanBlogs/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ethanBlogs/categories/category1/" class="nav-link"><i class="undefined"></i>
  category1
</a></li><li class="dropdown-item"><!----> <a href="/ethanBlogs/categories/category2/" class="nav-link"><i class="undefined"></i>
  category2
</a></li></ul></div></div><div class="nav-item"><a href="/ethanBlogs/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/ethanBlogs/timeline1/" class="nav-link"><i class="iconfont reco-date"></i>
  回忆线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ethanBlogs/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1aefc0b4></div> <aside class="sidebar" data-v-1aefc0b4><div class="personal-info-wrapper" data-v-39576ba9 data-v-1aefc0b4><img src="/ethanBlogs/avatar.png" alt="author-avatar" class="personal-img" data-v-39576ba9> <h3 class="name" data-v-39576ba9>
    ETHAN
  </h3> <div class="num" data-v-39576ba9><div data-v-39576ba9><h3 data-v-39576ba9>12</h3> <h6 data-v-39576ba9>Articles</h6></div> <div data-v-39576ba9><h3 data-v-39576ba9>4</h3> <h6 data-v-39576ba9>Tags</h6></div></div> <ul class="social-links" data-v-39576ba9></ul> <hr data-v-39576ba9></div> <nav class="nav-links"><div class="nav-item"><a href="/ethanBlogs/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ethanBlogs/categories/category1/" class="nav-link"><i class="undefined"></i>
  category1
</a></li><li class="dropdown-item"><!----> <a href="/ethanBlogs/categories/category2/" class="nav-link"><i class="undefined"></i>
  category2
</a></li></ul></div></div><div class="nav-item"><a href="/ethanBlogs/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/ethanBlogs/timeline1/" class="nav-link"><i class="iconfont reco-date"></i>
  回忆线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      工具
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/ethanBlogs/docs/theme-reco/" class="nav-link"><i class="undefined"></i>
  vuepress-reco
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/recoluan" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-25ba6db2 data-v-1aefc0b4><h3 class="title" data-v-25ba6db2 data-v-25ba6db2>VUE2硅谷知识点</h3> <!----> <label id="box" class="inputBox" data-v-25ba6db2 data-v-25ba6db2><input type="password" value="" data-v-25ba6db2> <span data-v-25ba6db2>Konck! Knock!</span> <button data-v-25ba6db2>OK</button></label> <div class="footer" data-v-25ba6db2 data-v-25ba6db2><span data-v-25ba6db2><i class="iconfont reco-theme" data-v-25ba6db2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-25ba6db2>vuePress-theme-reco</a></span> <span data-v-25ba6db2><i class="iconfont reco-copyright" data-v-25ba6db2></i> <a data-v-25ba6db2><span data-v-25ba6db2>ETHAN</span>
            
          <span data-v-25ba6db2>2017 - </span>
          2022
        </a></span></div></div> <div data-v-1aefc0b4><main class="page"><section><div class="page-title"><h1 class="title">VUE2硅谷知识点</h1> <div data-v-f875f3fc><i class="iconfont reco-account" data-v-f875f3fc><span data-v-f875f3fc>ETHAN</span></i> <i class="iconfont reco-date" data-v-f875f3fc><span data-v-f875f3fc>1/1/2022</span></i> <!----> <!----></div></div> <div class="theme-reco-content content__default"><h1 id="笔记"><a href="#笔记" class="header-anchor">#</a> 笔记</h1> <h2 id="脚手架文件结构"><a href="#脚手架文件结构" class="header-anchor">#</a> 脚手架文件结构</h2> <div class="language- extra-class"><pre><code>├── node_modules 
├── public
│   ├── favicon.ico: 页签图标
│   └── index.html: 主页面
├── src
│   ├── assets: 存放静态资源
│   │   └── logo.png
│   │── component: 存放组件
│   │   └── HelloWorld.vue
│   │── App.vue: 汇总所有组件
│   │── main.js: 入口文件
├── .gitignore: git版本管制忽略的配置
├── babel.config.js: babel的配置文件
├── package.json: 应用包配置文件 
├── README.md: 应用描述文件
├── package-lock.json：包版本控制文件
</code></pre></div><h2 id="关于不同版本的vue"><a href="#关于不同版本的vue" class="header-anchor">#</a> 关于不同版本的Vue</h2> <ol><li>vue.js与vue.runtime.xxx.js的区别：
<ol><li>vue.js是完整版的Vue，包含：核心功能 + 模板解析器。</li> <li>vue.runtime.xxx.js是运行版的Vue，只包含：核心功能；没有模板解析器。</li></ol></li> <li>因为vue.runtime.xxx.js没有模板解析器，所以不能使用template这个配置项，需要使用render函数接收到的createElement函数去指定具体内容。</li></ol> <h2 id="vue-config-js配置文件"><a href="#vue-config-js配置文件" class="header-anchor">#</a> vue.config.js配置文件</h2> <ol><li>使用vue inspect &gt; output.js可以查看到Vue脚手架的默认配置。</li> <li>使用vue.config.js可以对脚手架进行个性化定制，详情见：https://cli.vuejs.org/zh</li></ol> <h2 id="ref属性"><a href="#ref属性" class="header-anchor">#</a> ref属性</h2> <ol><li>被用来给元素或子组件注册引用信息（id的替代者）</li> <li>应用在html标签上获取的是真实DOM元素，应用在组件标签上是组件实例对象（vc）</li> <li>使用方式：
<ol><li>打标识：<code>&lt;h1 ref=&quot;xxx&quot;&gt;.....&lt;/h1&gt;</code> 或 <code>&lt;School ref=&quot;xxx&quot;&gt;&lt;/School&gt;</code></li> <li>获取：<code>this.$refs.xxx</code></li></ol></li></ol> <h2 id="props配置项"><a href="#props配置项" class="header-anchor">#</a> props配置项</h2> <ol><li><p>功能：让组件接收外部传过来的数据</p></li> <li><p>传递数据：<code>&lt;Demo name=&quot;xxx&quot;/&gt;</code></p></li> <li><p>接收数据：</p> <ol><li><p>第一种方式（只接收）：<code>props:['name']</code></p></li> <li><p>第二种方式（限制类型）：<code>props:{name:String}</code></p></li> <li><p>第三种方式（限制类型、限制必要性、指定默认值）：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>props<span class="token operator">:</span><span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token punctuation">{</span>
	type<span class="token operator">:</span>String<span class="token punctuation">,</span> <span class="token comment">//类型</span>
	required<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">//必要性</span>
	<span class="token keyword">default</span><span class="token operator">:</span><span class="token string">'老王'</span> <span class="token comment">//默认值</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li></ol> <blockquote><p>备注：props是只读的，Vue底层会监测你对props的修改，如果进行了修改，就会发出警告，若业务需求确实需要修改，那么请复制props的内容到data中一份，然后去修改data中的数据。</p></blockquote></li></ol> <h2 id="mixin-混入"><a href="#mixin-混入" class="header-anchor">#</a> mixin(混入)</h2> <ol><li><p>功能：可以把多个组件共用的配置提取成一个混入对象</p></li> <li><p>使用方式：</p> <p>第一步定义混合：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>{
    data(){....},
    methods:{....}
    ....
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>第二步使用混入：</p> <p>​	全局混入：<code>Vue.mixin(xxx)</code>
​	局部混入：<code>mixins:['xxx']</code></p></li></ol> <h2 id="插件"><a href="#插件" class="header-anchor">#</a> 插件</h2> <ol><li><p>功能：用于增强Vue</p></li> <li><p>本质：包含install方法的一个对象，install的第一个参数是Vue，第二个以后的参数是插件使用者传递的数据。</p></li> <li><p>定义插件：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>对象<span class="token punctuation">.</span><span class="token function-variable function">install</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">Vue<span class="token punctuation">,</span> options</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// 1. 添加全局过滤器</span>
    Vue<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">.</span><span class="token punctuation">)</span>

    <span class="token comment">// 2. 添加全局指令</span>
    Vue<span class="token punctuation">.</span><span class="token function">directive</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">.</span><span class="token punctuation">)</span>

    <span class="token comment">// 3. 配置全局混入(合)</span>
    Vue<span class="token punctuation">.</span><span class="token function">mixin</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">.</span><span class="token punctuation">)</span>

    <span class="token comment">// 4. 添加实例方法</span>
    <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span><span class="token function-variable function">$myMethod</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token operator">...</span><span class="token punctuation">}</span>
    <span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$myProperty <span class="token operator">=</span> xxxx
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div></li> <li><p>使用插件：<code>Vue.use()</code></p></li></ol> <h2 id="scoped样式"><a href="#scoped样式" class="header-anchor">#</a> scoped样式</h2> <ol><li>作用：让样式在局部生效，防止冲突。</li> <li>写法：<code>&lt;style scoped&gt;</code></li></ol> <h2 id="总结todolist案例"><a href="#总结todolist案例" class="header-anchor">#</a> 总结TodoList案例</h2> <ol><li><p>组件化编码流程：</p> <p>​	(1).拆分静态组件：组件要按照功能点拆分，命名不要与html元素冲突。</p> <p>​	(2).实现动态组件：考虑好数据的存放位置，数据是一个组件在用，还是一些组件在用：</p> <p>​			1).一个组件在用：放在组件自身即可。</p> <p>​			2). 一些组件在用：放在他们共同的父组件上（<span style="color:red;">状态提升</span>）。</p> <p>​	(3).实现交互：从绑定事件开始。</p></li> <li><p>props适用于：</p> <p>​	(1).父组件 ==&gt; 子组件 通信</p> <p>​	(2).子组件 ==&gt; 父组件 通信（要求父先给子一个函数）</p></li> <li><p>使用v-model时要切记：v-model绑定的值不能是props传过来的值，因为props是不可以修改的！</p></li> <li><p>props传过来的若是对象类型的值，修改对象中的属性时Vue不会报错，但不推荐这样做。</p></li></ol> <h2 id="webstorage"><a href="#webstorage" class="header-anchor">#</a> webStorage</h2> <ol><li><p>存储内容大小一般支持5MB左右（不同浏览器可能还不一样）</p></li> <li><p>浏览器端通过 Window.sessionStorage 和 Window.localStorage 属性来实现本地存储机制。</p></li> <li><p>相关API：</p> <ol><li><p><code>xxxxxStorage.setItem('key', 'value');</code>
该方法接受一个键和值作为参数，会把键值对添加到存储中，如果键名存在，则更新其对应的值。</p></li> <li><p><code>xxxxxStorage.getItem('person');</code></p> <p>​		该方法接受一个键名作为参数，返回键名对应的值。</p></li> <li><p><code>xxxxxStorage.removeItem('key');</code></p> <p>​		该方法接受一个键名作为参数，并把该键名从存储中删除。</p></li> <li><p><code>xxxxxStorage.clear()</code></p> <p>​		该方法会清空存储中的所有数据。</p></li></ol></li> <li><p>备注：</p> <ol><li>SessionStorage存储的内容会随着浏览器窗口关闭而消失。</li> <li>LocalStorage存储的内容，需要手动清除才会消失。</li> <li><code>xxxxxStorage.getItem(xxx)</code>如果xxx对应的value获取不到，那么getItem的返回值是null。</li> <li><code>JSON.parse(null)</code>的结果依然是null。</li></ol></li></ol> <h2 id="组件的自定义事件"><a href="#组件的自定义事件" class="header-anchor">#</a> 组件的自定义事件</h2> <ol><li><p>一种组件间通信的方式，适用于：<strong style="color:red;">子组件 ===&gt; 父组件</strong></p></li> <li><p>使用场景：A是父组件，B是子组件，B想给A传数据，那么就要在A中给B绑定自定义事件（<span style="color:red;">事件的回调在A中</span>）。</p></li> <li><p>绑定自定义事件：</p> <ol><li><p>第一种方式，在父组件中：<code>&lt;Demo @atguigu=&quot;test&quot;/&gt;</code>  或 <code>&lt;Demo v-on:atguigu=&quot;test&quot;/&gt;</code></p></li> <li><p>第二种方式，在父组件中：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">&lt;</span>Demo ref<span class="token operator">=</span><span class="token string">&quot;demo&quot;</span><span class="token operator">/</span><span class="token operator">&gt;</span>
<span class="token operator">...</span><span class="token operator">...</span>
<span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
   <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">.</span>xxx<span class="token punctuation">.</span><span class="token function">$on</span><span class="token punctuation">(</span><span class="token string">'atguigu'</span><span class="token punctuation">,</span><span class="token keyword">this</span><span class="token punctuation">.</span>test<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li> <li><p>若想让自定义事件只能触发一次，可以使用<code>once</code>修饰符，或<code>$once</code>方法。</p></li></ol></li> <li><p>触发自定义事件：<code>this.$emit('atguigu',数据)</code></p></li> <li><p>解绑自定义事件<code>this.$off('atguigu')</code></p></li> <li><p>组件上也可以绑定原生DOM事件，需要使用<code>native</code>修饰符。</p></li> <li><p>注意：通过<code>this.$refs.xxx.$on('atguigu',回调)</code>绑定自定义事件时，回调<span style="color:red;">要么配置在methods中</span>，<span style="color:red;">要么用箭头函数</span>，否则this指向会出问题！</p></li></ol> <h2 id="全局事件总线-globaleventbus"><a href="#全局事件总线-globaleventbus" class="header-anchor">#</a> 全局事件总线（GlobalEventBus）</h2> <ol><li><p>一种组件间通信的方式，适用于<span style="color:red;">任意组件间通信</span>。</p></li> <li><p>安装全局事件总线：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	<span class="token operator">...</span><span class="token operator">...</span>
	<span class="token function">beforeCreate</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token class-name">Vue</span><span class="token punctuation">.</span>prototype<span class="token punctuation">.</span>$bus <span class="token operator">=</span> <span class="token keyword">this</span> <span class="token comment">//安装全局事件总线，$bus就是当前应用的vm</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token operator">...</span><span class="token operator">...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p>使用事件总线：</p> <ol><li><p>接收数据：A组件想接收数据，则在A组件中给$bus绑定自定义事件，事件的<span style="color:red;">回调留在A组件自身。</span></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">methods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token function">demo</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token operator">...</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token operator">...</span><span class="token operator">...</span>
<span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>$bus<span class="token punctuation">.</span><span class="token function">$on</span><span class="token punctuation">(</span><span class="token string">'xxxx'</span><span class="token punctuation">,</span><span class="token keyword">this</span><span class="token punctuation">.</span>demo<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p>提供数据：<code>this.$bus.$emit('xxxx',数据)</code></p></li></ol></li> <li><p>最好在beforeDestroy钩子中，用$off去解绑<span style="color:red;">当前组件所用到的</span>事件。</p></li></ol> <h2 id="消息订阅与发布-pubsub"><a href="#消息订阅与发布-pubsub" class="header-anchor">#</a> 消息订阅与发布（pubsub）</h2> <ol><li><p>一种组件间通信的方式，适用于<span style="color:red;">任意组件间通信</span>。</p></li> <li><p>使用步骤：</p> <ol><li><p>安装pubsub：<code>npm i pubsub-js</code></p></li> <li><p>引入: <code>import pubsub from 'pubsub-js'</code></p></li> <li><p>接收数据：A组件想接收数据，则在A组件中订阅消息，订阅的<span style="color:red;">回调留在A组件自身。</span></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">methods</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  <span class="token function">demo</span><span class="token punctuation">(</span><span class="token parameter">data</span><span class="token punctuation">)</span><span class="token punctuation">{</span><span class="token operator">...</span><span class="token operator">...</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token operator">...</span><span class="token operator">...</span>
<span class="token function">mounted</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>pid <span class="token operator">=</span> pubsub<span class="token punctuation">.</span><span class="token function">subscribe</span><span class="token punctuation">(</span><span class="token string">'xxx'</span><span class="token punctuation">,</span><span class="token keyword">this</span><span class="token punctuation">.</span>demo<span class="token punctuation">)</span> <span class="token comment">//订阅消息</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p>提供数据：<code>pubsub.publish('xxx',数据)</code></p></li> <li><p>最好在beforeDestroy钩子中，用<code>PubSub.unsubscribe(pid)</code>去<span style="color:red;">取消订阅。</span></p></li></ol></li></ol> <h2 id="nexttick"><a href="#nexttick" class="header-anchor">#</a> nextTick</h2> <ol><li>语法：<code>this.$nextTick(回调函数)</code></li> <li>作用：在下一次 DOM 更新结束后执行其指定的回调。</li> <li>什么时候用：当改变数据后，要基于更新后的新DOM进行某些操作时，要在nextTick所指定的回调函数中执行。</li></ol> <h2 id="vue封装的过度与动画"><a href="#vue封装的过度与动画" class="header-anchor">#</a> Vue封装的过度与动画</h2> <ol><li><p>作用：在插入、更新或移除 DOM元素时，在合适的时候给元素添加样式类名。</p></li> <li><p>图示：<img src="https://img04.sogoucdn.com/app/a/100520146/5990c1dff7dc7a8fb3b34b4462bd0105" style="width:60%;"></p></li> <li><p>写法：</p> <ol><li><p>准备好样式：</p> <ul><li>元素进入的样式：
<ol><li>v-enter：进入的起点</li> <li>v-enter-active：进入过程中</li> <li>v-enter-to：进入的终点</li></ol></li> <li>元素离开的样式：
<ol><li>v-leave：离开的起点</li> <li>v-leave-active：离开过程中</li> <li>v-leave-to：离开的终点</li></ol></li></ul></li> <li><p>使用<code>&lt;transition&gt;</code>包裹要过度的元素，并配置name属性：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>transition</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>hello<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
	<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h1</span> <span class="token attr-name">v-show</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>isShow<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>你好啊！<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h1</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>transition</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li> <li><p>备注：若有多个元素需要过度，则需要使用：<code>&lt;transition-group&gt;</code>，且每个元素都要指定<code>key</code>值。</p></li></ol></li></ol> <h2 id="vue脚手架配置代理"><a href="#vue脚手架配置代理" class="header-anchor">#</a> vue脚手架配置代理</h2> <h3 id="方法一"><a href="#方法一" class="header-anchor">#</a> 方法一</h3> <p>​	在vue.config.js中添加如下配置：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>devServer<span class="token operator">:</span><span class="token punctuation">{</span>
  proxy<span class="token operator">:</span><span class="token string">&quot;http://localhost:5000&quot;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>说明：</p> <ol><li>优点：配置简单，请求资源时直接发给前端（8080）即可。</li> <li>缺点：不能配置多个代理，不能灵活的控制请求是否走代理。</li> <li>工作方式：若按照上述配置代理，当请求了前端不存在的资源时，那么该请求会转发给服务器 （优先匹配前端资源）</li></ol> <h3 id="方法二"><a href="#方法二" class="header-anchor">#</a> 方法二</h3> <p>​	编写vue.config.js配置具体代理规则：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
	devServer<span class="token operator">:</span> <span class="token punctuation">{</span>
      proxy<span class="token operator">:</span> <span class="token punctuation">{</span>
      <span class="token string">'/api1'</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token comment">// 匹配所有以 '/api1'开头的请求路径</span>
        target<span class="token operator">:</span> <span class="token string">'http://localhost:5000'</span><span class="token punctuation">,</span><span class="token comment">// 代理目标的基础路径</span>
        changeOrigin<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
        pathRewrite<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string">'^/api1'</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">}</span>
      <span class="token punctuation">}</span><span class="token punctuation">,</span>
      <span class="token string">'/api2'</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token comment">// 匹配所有以 '/api2'开头的请求路径</span>
        target<span class="token operator">:</span> <span class="token string">'http://localhost:5001'</span><span class="token punctuation">,</span><span class="token comment">// 代理目标的基础路径</span>
        changeOrigin<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
        pathRewrite<span class="token operator">:</span> <span class="token punctuation">{</span><span class="token string">'^/api2'</span><span class="token operator">:</span> <span class="token string">''</span><span class="token punctuation">}</span>
      <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment">/*
   changeOrigin设置为true时，服务器收到的请求头中的host为：localhost:5000
   changeOrigin设置为false时，服务器收到的请求头中的host为：localhost:8080
   changeOrigin默认值为true
*/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>说明：</p> <ol><li>优点：可以配置多个代理，且可以灵活的控制请求是否走代理。</li> <li>缺点：配置略微繁琐，请求资源时必须加前缀。</li></ol> <h2 id="插槽"><a href="#插槽" class="header-anchor">#</a> 插槽</h2> <ol><li><p>作用：让父组件可以向子组件指定位置插入html结构，也是一种组件间通信的方式，适用于 <strong style="color:red;">父组件 ===&gt; 子组件</strong> 。</p></li> <li><p>分类：默认插槽、具名插槽、作用域插槽</p></li> <li><p>使用方式：</p> <ol><li><p>默认插槽：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>父组件中：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Category</span><span class="token punctuation">&gt;</span></span>
           <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>html结构1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Category</span><span class="token punctuation">&gt;</span></span>
子组件中：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
               <span class="token comment">&lt;!-- 定义插槽 --&gt;</span>
               <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>slot</span><span class="token punctuation">&gt;</span></span>插槽默认内容...<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>slot</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></li> <li><p>具名插槽：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>父组件中：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Category</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>center<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
              <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>html结构1<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>

            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name"><span class="token namespace">v-slot:</span>footer</span><span class="token punctuation">&gt;</span></span>
               <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>html结构2<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Category</span><span class="token punctuation">&gt;</span></span>
子组件中：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
               <span class="token comment">&lt;!-- 定义插槽 --&gt;</span>
               <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>slot</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>center<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>插槽默认内容...<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>slot</span><span class="token punctuation">&gt;</span></span>
               <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>slot</span> <span class="token attr-name">name</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>footer<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>插槽默认内容...<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>slot</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div></li> <li><p>作用域插槽：</p> <ol><li><p>理解：<span style="color:red;">数据在组件的自身，但根据数据生成的结构需要组件的使用者来决定。</span>（games数据在Category组件中，但使用数据所遍历出来的结构由App组件决定）</p></li> <li><p>具体编码：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code>父组件中：
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Category</span><span class="token punctuation">&gt;</span></span>
			<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">scope</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>scopeData<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
				<span class="token comment">&lt;!-- 生成的是ul列表 --&gt;</span>
				<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>ul</span><span class="token punctuation">&gt;</span></span>
					<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>li</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>g in scopeData.games<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>g<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{g}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>li</span><span class="token punctuation">&gt;</span></span>
				<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>ul</span><span class="token punctuation">&gt;</span></span>
			<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Category</span><span class="token punctuation">&gt;</span></span>

		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Category</span><span class="token punctuation">&gt;</span></span>
			<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span> <span class="token attr-name">slot-scope</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>scopeData<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>
				<span class="token comment">&lt;!-- 生成的是h4标题 --&gt;</span>
				<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>h4</span> <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>g in scopeData.games<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>g<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>{{g}}<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>h4</span><span class="token punctuation">&gt;</span></span>
			<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
		<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Category</span><span class="token punctuation">&gt;</span></span>
子组件中：
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>template</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span><span class="token punctuation">&gt;</span></span>
                <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>slot</span> <span class="token attr-name">:games</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>games<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>slot</span><span class="token punctuation">&gt;</span></span>
            <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">&gt;</span></span>
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>template</span><span class="token punctuation">&gt;</span></span>
		
        <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span><span class="token punctuation">&gt;</span></span><span class="token script"><span class="token language-javascript">
            <span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token punctuation">{</span>
                name<span class="token operator">:</span><span class="token string">'Category'</span><span class="token punctuation">,</span>
                props<span class="token operator">:</span><span class="token punctuation">[</span><span class="token string">'title'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
                <span class="token comment">//数据在子组件自身</span>
                <span class="token function">data</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">return</span> <span class="token punctuation">{</span>
                        games<span class="token operator">:</span><span class="token punctuation">[</span><span class="token string">'红色警戒'</span><span class="token punctuation">,</span><span class="token string">'穿越火线'</span><span class="token punctuation">,</span><span class="token string">'劲舞团'</span><span class="token punctuation">,</span><span class="token string">'超级玛丽'</span><span class="token punctuation">]</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">,</span>
            <span class="token punctuation">}</span>
        </span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div></li></ol></li></ol> <div class="language- line-numbers-mode"><pre class="language-text"><code>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ol> <h2 id="vuex"><a href="#vuex" class="header-anchor">#</a> Vuex</h2> <h3 id="_1-概念"><a href="#_1-概念" class="header-anchor">#</a> 1.概念</h3> <p>​		在Vue中实现集中式状态（数据）管理的一个Vue插件，对vue应用中多个组件的共享状态进行集中式的管理（读/写），也是一种组件间通信的方式，且适用于任意组件间通信。</p> <h3 id="_2-何时使用"><a href="#_2-何时使用" class="header-anchor">#</a> 2.何时使用？</h3> <p>​		多个组件需要共享数据时</p> <h3 id="_3-搭建vuex环境"><a href="#_3-搭建vuex环境" class="header-anchor">#</a> 3.搭建vuex环境</h3> <ol><li><p>创建文件：<code>src/store/index.js</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//引入Vue核心库</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token comment">//引入Vuex</span>
<span class="token keyword">import</span> Vuex <span class="token keyword">from</span> <span class="token string">'vuex'</span>
<span class="token comment">//应用Vuex插件</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>Vuex<span class="token punctuation">)</span>

<span class="token comment">//准备actions对象——响应组件中用户的动作</span>
<span class="token keyword">const</span> actions <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token comment">//准备mutations对象——修改state中的数据</span>
<span class="token keyword">const</span> mutations <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token comment">//准备state对象——保存具体的数据</span>
<span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment">//创建并暴露store</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	actions<span class="token punctuation">,</span>
	mutations<span class="token punctuation">,</span>
	state
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></li> <li><p>在<code>main.js</code>中创建vm时传入<code>store</code>配置项</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">...</span><span class="token operator">...</span>
<span class="token comment">//引入store</span>
<span class="token keyword">import</span> store <span class="token keyword">from</span> <span class="token string">'./store'</span>
<span class="token operator">...</span><span class="token operator">...</span>

<span class="token comment">//创建vm</span>
<span class="token keyword">new</span> <span class="token class-name">Vue</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	el<span class="token operator">:</span><span class="token string">'#app'</span><span class="token punctuation">,</span>
	<span class="token function-variable function">render</span><span class="token operator">:</span> <span class="token parameter">h</span> <span class="token operator">=&gt;</span> <span class="token function">h</span><span class="token punctuation">(</span>App<span class="token punctuation">)</span><span class="token punctuation">,</span>
	store
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div></li></ol> <h3 id="_4-基本使用"><a href="#_4-基本使用" class="header-anchor">#</a> 4.基本使用</h3> <ol><li><p>初始化数据、配置<code>actions</code>、配置<code>mutations</code>，操作文件<code>store.js</code></p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//引入Vue核心库</span>
<span class="token keyword">import</span> Vue <span class="token keyword">from</span> <span class="token string">'vue'</span>
<span class="token comment">//引入Vuex</span>
<span class="token keyword">import</span> Vuex <span class="token keyword">from</span> <span class="token string">'vuex'</span>
<span class="token comment">//引用Vuex</span>
Vue<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>Vuex<span class="token punctuation">)</span>

<span class="token keyword">const</span> actions <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">//响应组件中加的动作</span>
	<span class="token function">jia</span><span class="token punctuation">(</span><span class="token parameter">context<span class="token punctuation">,</span>value</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token comment">// console.log('actions中的jia被调用了',miniStore,value)</span>
		context<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'JIA'</span><span class="token punctuation">,</span>value<span class="token punctuation">)</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> mutations <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token comment">//执行加</span>
	<span class="token constant">JIA</span><span class="token punctuation">(</span><span class="token parameter">state<span class="token punctuation">,</span>value</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token comment">// console.log('mutations中的JIA被调用了',state,value)</span>
		state<span class="token punctuation">.</span>sum <span class="token operator">+=</span> value
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">//初始化数据</span>
<span class="token keyword">const</span> state <span class="token operator">=</span> <span class="token punctuation">{</span>
   sum<span class="token operator">:</span><span class="token number">0</span>
<span class="token punctuation">}</span>

<span class="token comment">//创建并暴露store</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	actions<span class="token punctuation">,</span>
	mutations<span class="token punctuation">,</span>
	state<span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div></li> <li><p>组件中读取vuex中的数据：<code>$store.state.sum</code></p></li> <li><p>组件中修改vuex中的数据：<code>$store.dispatch('action中的方法名',数据)</code> 或 <code>$store.commit('mutations中的方法名',数据)</code></p> <blockquote><p>备注：若没有网络请求或其他业务逻辑，组件中也可以越过actions，即不写<code>dispatch</code>，直接编写<code>commit</code></p></blockquote></li></ol> <h3 id="_5-getters的使用"><a href="#_5-getters的使用" class="header-anchor">#</a> 5.getters的使用</h3> <ol><li><p>概念：当state中的数据需要经过加工后再使用时，可以使用getters加工。</p></li> <li><p>在<code>store.js</code>中追加<code>getters</code>配置</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token operator">...</span><span class="token operator">...</span>

<span class="token keyword">const</span> getters <span class="token operator">=</span> <span class="token punctuation">{</span>
	<span class="token function">bigSum</span><span class="token punctuation">(</span><span class="token parameter">state</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> state<span class="token punctuation">.</span>sum <span class="token operator">*</span> <span class="token number">10</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">//创建并暴露store</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	<span class="token operator">...</span><span class="token operator">...</span>
	getters
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p>组件中读取数据：<code>$store.getters.bigSum</code></p></li></ol> <h3 id="_6-四个map方法的使用"><a href="#_6-四个map方法的使用" class="header-anchor">#</a> 6.四个map方法的使用</h3> <ol><li><p><strong>mapState方法：</strong>用于帮助我们映射<code>state</code>中的数据为计算属性</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>computed<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token comment">//借助mapState生成计算属性：sum、school、subject（对象写法）</span>
     <span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">{</span>sum<span class="token operator">:</span><span class="token string">'sum'</span><span class="token punctuation">,</span>school<span class="token operator">:</span><span class="token string">'school'</span><span class="token punctuation">,</span>subject<span class="token operator">:</span><span class="token string">'subject'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
         
    <span class="token comment">//借助mapState生成计算属性：sum、school、subject（数组写法）</span>
    <span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'sum'</span><span class="token punctuation">,</span><span class="token string">'school'</span><span class="token punctuation">,</span><span class="token string">'subject'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p><strong>mapGetters方法：</strong>用于帮助我们映射<code>getters</code>中的数据为计算属性</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>computed<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token comment">//借助mapGetters生成计算属性：bigSum（对象写法）</span>
    <span class="token operator">...</span><span class="token function">mapGetters</span><span class="token punctuation">(</span><span class="token punctuation">{</span>bigSum<span class="token operator">:</span><span class="token string">'bigSum'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>

    <span class="token comment">//借助mapGetters生成计算属性：bigSum（数组写法）</span>
    <span class="token operator">...</span><span class="token function">mapGetters</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'bigSum'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p><strong>mapActions方法：</strong>用于帮助我们生成与<code>actions</code>对话的方法，即：包含<code>$store.dispatch(xxx)</code>的函数</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>methods<span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token comment">//靠mapActions生成：incrementOdd、incrementWait（对象形式）</span>
    <span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token punctuation">{</span>incrementOdd<span class="token operator">:</span><span class="token string">'jiaOdd'</span><span class="token punctuation">,</span>incrementWait<span class="token operator">:</span><span class="token string">'jiaWait'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>

    <span class="token comment">//靠mapActions生成：incrementOdd、incrementWait（数组形式）</span>
    <span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'jiaOdd'</span><span class="token punctuation">,</span><span class="token string">'jiaWait'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li> <li><p><strong>mapMutations方法：</strong>用于帮助我们生成与<code>mutations</code>对话的方法，即：包含<code>$store.commit(xxx)</code>的函数</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>methods<span class="token operator">:</span><span class="token punctuation">{</span>
    <span class="token comment">//靠mapActions生成：increment、decrement（对象形式）</span>
    <span class="token operator">...</span><span class="token function">mapMutations</span><span class="token punctuation">(</span><span class="token punctuation">{</span>increment<span class="token operator">:</span><span class="token string">'JIA'</span><span class="token punctuation">,</span>decrement<span class="token operator">:</span><span class="token string">'JIAN'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    
    <span class="token comment">//靠mapMutations生成：JIA、JIAN（对象形式）</span>
    <span class="token operator">...</span><span class="token function">mapMutations</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'JIA'</span><span class="token punctuation">,</span><span class="token string">'JIAN'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></li></ol> <blockquote><p>备注：mapActions与mapMutations使用时，若需要传递参数需要：在模板中绑定事件时传递好参数，否则参数是事件对象。</p></blockquote> <h3 id="_7-模块化-命名空间"><a href="#_7-模块化-命名空间" class="header-anchor">#</a> 7.模块化+命名空间</h3> <ol><li><p>目的：让代码更好维护，让多种数据分类更加明确。</p></li> <li><p>修改<code>store.js</code></p> <div class="language-javascript line-numbers-mode"><pre class="language-javascript"><code><span class="token keyword">const</span> countAbout <span class="token operator">=</span> <span class="token punctuation">{</span>
  namespaced<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//开启命名空间</span>
  state<span class="token operator">:</span><span class="token punctuation">{</span>x<span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">,</span>
  mutations<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  actions<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  getters<span class="token operator">:</span> <span class="token punctuation">{</span>
    <span class="token function">bigSum</span><span class="token punctuation">(</span><span class="token parameter">state</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
       <span class="token keyword">return</span> state<span class="token punctuation">.</span>sum <span class="token operator">*</span> <span class="token number">10</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> personAbout <span class="token operator">=</span> <span class="token punctuation">{</span>
  namespaced<span class="token operator">:</span><span class="token boolean">true</span><span class="token punctuation">,</span><span class="token comment">//开启命名空间</span>
  state<span class="token operator">:</span><span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  mutations<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span><span class="token punctuation">,</span>
  actions<span class="token operator">:</span> <span class="token punctuation">{</span> <span class="token operator">...</span> <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">const</span> store <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Vuex<span class="token punctuation">.</span>Store</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  modules<span class="token operator">:</span> <span class="token punctuation">{</span>
    countAbout<span class="token punctuation">,</span>
    personAbout
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div></li> <li><p>开启命名空间后，组件中读取state数据：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//方式一：自己直接读取</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span>state<span class="token punctuation">.</span>personAbout<span class="token punctuation">.</span>list
<span class="token comment">//方式二：借助mapState读取：</span>
<span class="token operator">...</span><span class="token function">mapState</span><span class="token punctuation">(</span><span class="token string">'countAbout'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'sum'</span><span class="token punctuation">,</span><span class="token string">'school'</span><span class="token punctuation">,</span><span class="token string">'subject'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>开启命名空间后，组件中读取getters数据：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//方式一：自己直接读取</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span>getters<span class="token punctuation">[</span><span class="token string">'personAbout/firstPersonName'</span><span class="token punctuation">]</span>
<span class="token comment">//方式二：借助mapGetters读取：</span>
<span class="token operator">...</span><span class="token function">mapGetters</span><span class="token punctuation">(</span><span class="token string">'countAbout'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'bigSum'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>开启命名空间后，组件中调用dispatch</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//方式一：自己直接dispatch</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token string">'personAbout/addPersonWang'</span><span class="token punctuation">,</span>person<span class="token punctuation">)</span>
<span class="token comment">//方式二：借助mapActions：</span>
<span class="token operator">...</span><span class="token function">mapActions</span><span class="token punctuation">(</span><span class="token string">'countAbout'</span><span class="token punctuation">,</span><span class="token punctuation">{</span>incrementOdd<span class="token operator">:</span><span class="token string">'jiaOdd'</span><span class="token punctuation">,</span>incrementWait<span class="token operator">:</span><span class="token string">'jiaWait'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li> <li><p>开启命名空间后，组件中调用commit</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//方式一：自己直接commit</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$store<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token string">'personAbout/ADD_PERSON'</span><span class="token punctuation">,</span>person<span class="token punctuation">)</span>
<span class="token comment">//方式二：借助mapMutations：</span>
<span class="token operator">...</span><span class="token function">mapMutations</span><span class="token punctuation">(</span><span class="token string">'countAbout'</span><span class="token punctuation">,</span><span class="token punctuation">{</span>increment<span class="token operator">:</span><span class="token string">'JIA'</span><span class="token punctuation">,</span>decrement<span class="token operator">:</span><span class="token string">'JIAN'</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li></ol> <h2 id="路由"><a href="#路由" class="header-anchor">#</a> 路由</h2> <ol><li>理解： 一个路由（route）就是一组映射关系（key - value），多个路由需要路由器（router）进行管理。</li> <li>前端路由：key是路径，value是组件。</li></ol> <h3 id="_1-基本使用"><a href="#_1-基本使用" class="header-anchor">#</a> 1.基本使用</h3> <ol><li><p>安装vue-router，命令：<code>npm i vue-router</code></p></li> <li><p>应用插件：<code>Vue.use(VueRouter)</code></p></li> <li><p>编写router配置项:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//引入VueRouter</span>
<span class="token keyword">import</span> VueRouter <span class="token keyword">from</span> <span class="token string">'vue-router'</span>
<span class="token comment">//引入Luyou 组件</span>
<span class="token keyword">import</span> About <span class="token keyword">from</span> <span class="token string">'../components/About'</span>
<span class="token keyword">import</span> Home <span class="token keyword">from</span> <span class="token string">'../components/Home'</span>

<span class="token comment">//创建router实例对象，去管理一组一组的路由规则</span>
<span class="token keyword">const</span> router <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">VueRouter</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	routes<span class="token operator">:</span><span class="token punctuation">[</span>
		<span class="token punctuation">{</span>
			path<span class="token operator">:</span><span class="token string">'/about'</span><span class="token punctuation">,</span>
			component<span class="token operator">:</span>About
            <span class="token comment">// component: () =&gt; import('@/views/login.vue') //按需导入</span>
            
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>
			path<span class="token operator">:</span><span class="token string">'/home'</span><span class="token punctuation">,</span>
			component<span class="token operator">:</span>Home
		<span class="token punctuation">}</span>
	<span class="token punctuation">]</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//暴露router</span>
<span class="token keyword">export</span> <span class="token keyword">default</span> router
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></li> <li><p>实现切换（active-class可配置高亮样式）</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">active-class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>active<span class="token punctuation">&quot;</span></span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/about<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>About<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>指定展示位置</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ol> <h3 id="_2-几个注意点"><a href="#_2-几个注意点" class="header-anchor">#</a> 2.几个注意点</h3> <ol><li>路由组件通常存放在<code>pages</code>文件夹，一般组件通常存放在<code>components</code>文件夹。</li> <li>通过切换，“隐藏”了的路由组件，默认是被销毁掉的，需要的时候再去挂载。</li> <li>每个组件都有自己的<code>$route</code>属性，里面存储着自己的路由信息。</li> <li>整个应用只有一个router，可以通过组件的<code>$router</code>属性获取到。</li></ol> <h3 id="_3-多级路由-多级路由"><a href="#_3-多级路由-多级路由" class="header-anchor">#</a> 3.多级路由（多级路由）</h3> <ol><li><p>配置路由规则，使用children配置项：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>routes<span class="token operator">:</span><span class="token punctuation">[</span>
	<span class="token punctuation">{</span>
		path<span class="token operator">:</span><span class="token string">'/about'</span><span class="token punctuation">,</span>
		component<span class="token operator">:</span>About<span class="token punctuation">,</span>
	<span class="token punctuation">}</span><span class="token punctuation">,</span>
	<span class="token punctuation">{</span>
		path<span class="token operator">:</span><span class="token string">'/home'</span><span class="token punctuation">,</span>
		component<span class="token operator">:</span>Home<span class="token punctuation">,</span>
		children<span class="token operator">:</span><span class="token punctuation">[</span> <span class="token comment">//通过children配置子级路由</span>
			<span class="token punctuation">{</span>
				path<span class="token operator">:</span><span class="token string">'news'</span><span class="token punctuation">,</span> <span class="token comment">//此处一定不要写：/news</span>
				component<span class="token operator">:</span>News
			<span class="token punctuation">}</span><span class="token punctuation">,</span>
			<span class="token punctuation">{</span>
				path<span class="token operator">:</span><span class="token string">'message'</span><span class="token punctuation">,</span><span class="token comment">//此处一定不要写：/message</span>
				component<span class="token operator">:</span>Message
			<span class="token punctuation">}</span>
		<span class="token punctuation">]</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></li> <li><p>跳转（要写完整路径）：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/home/news<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>News<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ol> <h3 id="_4-路由的query参数"><a href="#_4-路由的query参数" class="header-anchor">#</a> 4.路由的query参数</h3> <ol><li><p>传递参数</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token comment">&lt;!-- 跳转并携带query参数，to的字符串写法 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/home/message/detail?id=666&amp;title=你好<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
				
<span class="token comment">&lt;!-- 跳转并携带query参数，to的对象写法 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> 
	<span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{
		path:<span class="token punctuation">'</span>/home/message/detail<span class="token punctuation">'</span>,
		query:{
		   id:666,
            title:<span class="token punctuation">'</span>你好<span class="token punctuation">'</span>
		}
	}<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p>接收参数：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>$route<span class="token punctuation">.</span>query<span class="token punctuation">.</span>id
$route<span class="token punctuation">.</span>query<span class="token punctuation">.</span>title
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ol> <h3 id="_5-命名路由"><a href="#_5-命名路由" class="header-anchor">#</a> 5.命名路由</h3> <ol><li><p>作用：可以简化路由的跳转。</p></li> <li><p>如何使用</p> <ol><li><p>给路由命名：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
	path<span class="token operator">:</span><span class="token string">'/demo'</span><span class="token punctuation">,</span>
	component<span class="token operator">:</span>Demo<span class="token punctuation">,</span>
	children<span class="token operator">:</span><span class="token punctuation">[</span>
		<span class="token punctuation">{</span>
			path<span class="token operator">:</span><span class="token string">'test'</span><span class="token punctuation">,</span>
			component<span class="token operator">:</span>Test<span class="token punctuation">,</span>
			children<span class="token operator">:</span><span class="token punctuation">[</span>
				<span class="token punctuation">{</span>
                      name<span class="token operator">:</span><span class="token string">'hello'</span> <span class="token comment">//给路由命名</span>
					path<span class="token operator">:</span><span class="token string">'welcome'</span><span class="token punctuation">,</span>
					component<span class="token operator">:</span>Hello<span class="token punctuation">,</span>
				<span class="token punctuation">}</span>
			<span class="token punctuation">]</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div></li> <li><p>简化跳转：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token comment">&lt;!--简化前，需要写完整的路径 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/demo/test/welcome<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!--简化后，直接通过名字跳转 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{name:<span class="token punctuation">'</span>hello<span class="token punctuation">'</span>}<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>

<span class="token comment">&lt;!--简化写法配合传递参数 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> 
	<span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{
		name:<span class="token punctuation">'</span>hello<span class="token punctuation">'</span>,
		query:{
		   id:666,
            title:<span class="token punctuation">'</span>你好<span class="token punctuation">'</span>
		}
	}<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div></li></ol></li></ol> <h3 id="_6-路由的params参数"><a href="#_6-路由的params参数" class="header-anchor">#</a> 6.路由的params参数</h3> <ol><li><p>配置路由，声明接收params参数</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
	path<span class="token operator">:</span><span class="token string">'/home'</span><span class="token punctuation">,</span>
	component<span class="token operator">:</span>Home<span class="token punctuation">,</span>
	children<span class="token operator">:</span><span class="token punctuation">[</span>
		<span class="token punctuation">{</span>
			path<span class="token operator">:</span><span class="token string">'news'</span><span class="token punctuation">,</span>
			component<span class="token operator">:</span>News
		<span class="token punctuation">}</span><span class="token punctuation">,</span>
		<span class="token punctuation">{</span>
			component<span class="token operator">:</span>Message<span class="token punctuation">,</span>
			children<span class="token operator">:</span><span class="token punctuation">[</span>
				<span class="token punctuation">{</span>
					name<span class="token operator">:</span><span class="token string">'xiangqing'</span><span class="token punctuation">,</span>
					path<span class="token operator">:</span><span class="token string">'detail/:id/:title'</span><span class="token punctuation">,</span> <span class="token comment">//使用占位符声明接收params参数</span>
					component<span class="token operator">:</span>Detail
				<span class="token punctuation">}</span>
			<span class="token punctuation">]</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div></li> <li><p>传递参数</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token comment">&lt;!-- 跳转并携带params参数，to的字符串写法 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> <span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>/home/message/detail/666/你好<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
				
<span class="token comment">&lt;!-- 跳转并携带params参数，to的对象写法 --&gt;</span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-link</span> 
	<span class="token attr-name">:to</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>{
		name:<span class="token punctuation">'</span>xiangqing<span class="token punctuation">'</span>,
		params:{
		   id:666,
            title:<span class="token punctuation">'</span>你好<span class="token punctuation">'</span>
		}
	}<span class="token punctuation">&quot;</span></span>
<span class="token punctuation">&gt;</span></span>跳转<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-link</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><blockquote><p>特别注意：路由携带params参数时，若使用to的对象写法，则不能使用path配置项，必须使用name配置！</p></blockquote></li> <li><p>接收参数：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code>$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>id
$route<span class="token punctuation">.</span>params<span class="token punctuation">.</span>title
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ol> <h3 id="_7-路由的props配置"><a href="#_7-路由的props配置" class="header-anchor">#</a> 7.路由的props配置</h3> <p>​	作用：让路由组件更方便的收到参数</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token string">'xiangqing'</span><span class="token punctuation">,</span>
	path<span class="token operator">:</span><span class="token string">'detail/:id'</span><span class="token punctuation">,</span>
	component<span class="token operator">:</span>Detail<span class="token punctuation">,</span>

	<span class="token comment">//第一种写法：props值为对象，该对象中所有的key-value的组合最终都会通过props传给Detail组件</span>
	<span class="token comment">// props:{a:900}</span>

	<span class="token comment">//第二种写法：props值为布尔值，布尔值为true，则把路由收到的所有params参数通过props传给Detail组件</span>
	<span class="token comment">// props:true</span>
	
	<span class="token comment">//第三种写法：props值为函数，该函数返回的对象中每一组key-value都会通过props传给Detail组件</span>
	<span class="token function">props</span><span class="token punctuation">(</span><span class="token parameter">route</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token punctuation">{</span>
			id<span class="token operator">:</span>route<span class="token punctuation">.</span>query<span class="token punctuation">.</span>id<span class="token punctuation">,</span>
			title<span class="token operator">:</span>route<span class="token punctuation">.</span>query<span class="token punctuation">.</span>title
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h3 id="_8-router-link-的replace属性"><a href="#_8-router-link-的replace属性" class="header-anchor">#</a> 8.<code>&lt;router-link&gt;</code>的replace属性</h3> <ol><li>作用：控制路由跳转时操作浏览器历史记录的模式</li> <li>浏览器的历史记录有两种写入方式：分别为<code>push</code>和<code>replace</code>，<code>push</code>是追加历史记录，<code>replace</code>是替换当前记录。路由跳转时候默认为<code>push</code></li> <li>如何开启<code>replace</code>模式：<code>&lt;router-link replace .......&gt;News&lt;/router-link&gt;</code></li></ol> <h3 id="_9-编程式路由导航"><a href="#_9-编程式路由导航" class="header-anchor">#</a> 9.编程式路由导航</h3> <ol><li><p>作用：不借助<code>&lt;router-link&gt;</code>实现路由跳转，让路由跳转更加灵活</p></li> <li><p>具体编码：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//$router的两个API</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token string">'xiangqing'</span><span class="token punctuation">,</span>
		params<span class="token operator">:</span><span class="token punctuation">{</span>
			id<span class="token operator">:</span>xxx<span class="token punctuation">,</span>
			title<span class="token operator">:</span>xxx
		<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	name<span class="token operator">:</span><span class="token string">'xiangqing'</span><span class="token punctuation">,</span>
		params<span class="token operator">:</span><span class="token punctuation">{</span>
			id<span class="token operator">:</span>xxx<span class="token punctuation">,</span>
			title<span class="token operator">:</span>xxx
		<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//前进</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//后退</span>
<span class="token keyword">this</span><span class="token punctuation">.</span>$router<span class="token punctuation">.</span><span class="token function">go</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//可前进也可后退</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div></li></ol> <h3 id="_10-缓存路由组件"><a href="#_10-缓存路由组件" class="header-anchor">#</a> 10.缓存路由组件</h3> <ol><li><p>作用：让不展示的路由组件保持挂载，不被销毁。</p></li> <li><p>具体编码：</p> <div class="language-vue line-numbers-mode"><pre class="language-vue"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>keep-alive</span> <span class="token attr-name">include</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>News<span class="token punctuation">&quot;</span></span><span class="token punctuation">&gt;</span></span> 
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>router-view</span><span class="token punctuation">&gt;</span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>router-view</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>keep-alive</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div></li></ol> <h3 id="_11-两个新的生命周期钩子"><a href="#_11-两个新的生命周期钩子" class="header-anchor">#</a> 11.两个新的生命周期钩子</h3> <ol><li>作用：路由组件所独有的两个钩子，用于捕获路由组件的激活状态。</li> <li>具体名字：
<ol><li><code>activated</code>路由组件被激活时触发。</li> <li><code>deactivated</code>路由组件失活时触发。</li></ol></li></ol> <h3 id="_12-路由守卫"><a href="#_12-路由守卫" class="header-anchor">#</a> 12.路由守卫</h3> <ol><li><p>作用：对路由进行权限控制</p></li> <li><p>分类：全局守卫、独享守卫、组件内守卫</p></li> <li><p>全局守卫:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//全局前置守卫：初始化时执行、每次路由切换前执行</span>
router<span class="token punctuation">.</span><span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span>from<span class="token punctuation">,</span>next</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'beforeEach'</span><span class="token punctuation">,</span>to<span class="token punctuation">,</span>from<span class="token punctuation">)</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>isAuth<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//判断当前路由是否需要进行权限控制</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">'school'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'atguigu'</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//权限控制的具体规则</span>
			<span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//放行</span>
		<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
			<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'暂无权限查看'</span><span class="token punctuation">)</span>
			<span class="token comment">// next({name:'guanyu'})</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
		<span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//放行</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

<span class="token comment">//全局后置守卫：初始化时执行、每次路由切换后执行</span>
router<span class="token punctuation">.</span><span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span><span class="token keyword">from</span></span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'afterEach'</span><span class="token punctuation">,</span>to<span class="token punctuation">,</span>from<span class="token punctuation">)</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>title<span class="token punctuation">)</span><span class="token punctuation">{</span> 
		document<span class="token punctuation">.</span>title <span class="token operator">=</span> to<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>title <span class="token comment">//修改网页的title</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
		document<span class="token punctuation">.</span>title <span class="token operator">=</span> <span class="token string">'vue_test'</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div></li> <li><p>独享守卫:</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token function">beforeEnter</span><span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span>from<span class="token punctuation">,</span>next</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
	console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'beforeEnter'</span><span class="token punctuation">,</span>to<span class="token punctuation">,</span>from<span class="token punctuation">)</span>
	<span class="token keyword">if</span><span class="token punctuation">(</span>to<span class="token punctuation">.</span>meta<span class="token punctuation">.</span>isAuth<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token comment">//判断当前路由是否需要进行权限控制</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>localStorage<span class="token punctuation">.</span><span class="token function">getItem</span><span class="token punctuation">(</span><span class="token string">'school'</span><span class="token punctuation">)</span> <span class="token operator">===</span> <span class="token string">'atguigu'</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
			<span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
			<span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">'暂无权限查看'</span><span class="token punctuation">)</span>
			<span class="token comment">// next({name:'guanyu'})</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span><span class="token keyword">else</span><span class="token punctuation">{</span>
		<span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div></li> <li><p>组件内守卫：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">//进入守卫：通过路由规则，进入该组件时被调用</span>
<span class="token function">beforeRouteEnter</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
<span class="token comment">//离开守卫：通过路由规则，离开该组件时被调用</span>
<span class="token function">beforeRouteLeave</span> <span class="token punctuation">(</span><span class="token parameter">to<span class="token punctuation">,</span> from<span class="token punctuation">,</span> next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ol> <h3 id="_13-路由器的两种工作模式"><a href="#_13-路由器的两种工作模式" class="header-anchor">#</a> 13.路由器的两种工作模式</h3> <ol><li>对于一个url来说，什么是hash值？—— #及其后面的内容就是hash值。</li> <li>hash值不会包含在 HTTP 请求中，即：hash值不会带给服务器。</li> <li>hash模式：
<ol><li>地址中永远带着#号，不美观 。</li> <li>若以后将地址通过第三方手机app分享，若app校验严格，则地址会被标记为不合法。</li> <li>兼容性较好。</li></ol></li> <li>history模式：
<ol><li>地址干净，美观 。</li> <li>兼容性和hash模式相比略差。</li> <li>应用部署上线时需要后端人员支持，解决刷新页面服务端404的问题。</li></ol></li></ol></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">1/2/2022, 5:22:04 PM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-cb1513f6><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#脚手架文件结构" class="sidebar-link reco-side-脚手架文件结构" data-v-cb1513f6>脚手架文件结构</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#关于不同版本的vue" class="sidebar-link reco-side-关于不同版本的vue" data-v-cb1513f6>关于不同版本的Vue</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#vue-config-js配置文件" class="sidebar-link reco-side-vue-config-js配置文件" data-v-cb1513f6>vue.config.js配置文件</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#ref属性" class="sidebar-link reco-side-ref属性" data-v-cb1513f6>ref属性</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#props配置项" class="sidebar-link reco-side-props配置项" data-v-cb1513f6>props配置项</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#mixin-混入" class="sidebar-link reco-side-mixin-混入" data-v-cb1513f6>mixin(混入)</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#插件" class="sidebar-link reco-side-插件" data-v-cb1513f6>插件</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#scoped样式" class="sidebar-link reco-side-scoped样式" data-v-cb1513f6>scoped样式</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#总结todolist案例" class="sidebar-link reco-side-总结todolist案例" data-v-cb1513f6>总结TodoList案例</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#webstorage" class="sidebar-link reco-side-webstorage" data-v-cb1513f6>webStorage</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#组件的自定义事件" class="sidebar-link reco-side-组件的自定义事件" data-v-cb1513f6>组件的自定义事件</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#全局事件总线-globaleventbus" class="sidebar-link reco-side-全局事件总线-globaleventbus" data-v-cb1513f6>全局事件总线（GlobalEventBus）</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#消息订阅与发布-pubsub" class="sidebar-link reco-side-消息订阅与发布-pubsub" data-v-cb1513f6>消息订阅与发布（pubsub）</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#nexttick" class="sidebar-link reco-side-nexttick" data-v-cb1513f6>nextTick</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#vue封装的过度与动画" class="sidebar-link reco-side-vue封装的过度与动画" data-v-cb1513f6>Vue封装的过度与动画</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#vue脚手架配置代理" class="sidebar-link reco-side-vue脚手架配置代理" data-v-cb1513f6>vue脚手架配置代理</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#方法一" class="sidebar-link reco-side-方法一" data-v-cb1513f6>方法一</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#方法二" class="sidebar-link reco-side-方法二" data-v-cb1513f6>方法二</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#插槽" class="sidebar-link reco-side-插槽" data-v-cb1513f6>插槽</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#vuex" class="sidebar-link reco-side-vuex" data-v-cb1513f6>Vuex</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_1-概念" class="sidebar-link reco-side-_1-概念" data-v-cb1513f6>1.概念</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_2-何时使用" class="sidebar-link reco-side-_2-何时使用" data-v-cb1513f6>2.何时使用？</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_3-搭建vuex环境" class="sidebar-link reco-side-_3-搭建vuex环境" data-v-cb1513f6>3.搭建vuex环境</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_4-基本使用" class="sidebar-link reco-side-_4-基本使用" data-v-cb1513f6>4.基本使用</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_5-getters的使用" class="sidebar-link reco-side-_5-getters的使用" data-v-cb1513f6>5.getters的使用</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_6-四个map方法的使用" class="sidebar-link reco-side-_6-四个map方法的使用" data-v-cb1513f6>6.四个map方法的使用</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_7-模块化-命名空间" class="sidebar-link reco-side-_7-模块化-命名空间" data-v-cb1513f6>7.模块化+命名空间</a></li><li class="level-2" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#路由" class="sidebar-link reco-side-路由" data-v-cb1513f6>路由</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_1-基本使用" class="sidebar-link reco-side-_1-基本使用" data-v-cb1513f6>1.基本使用</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_2-几个注意点" class="sidebar-link reco-side-_2-几个注意点" data-v-cb1513f6>2.几个注意点</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_3-多级路由-多级路由" class="sidebar-link reco-side-_3-多级路由-多级路由" data-v-cb1513f6>3.多级路由（多级路由）</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_4-路由的query参数" class="sidebar-link reco-side-_4-路由的query参数" data-v-cb1513f6>4.路由的query参数</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_5-命名路由" class="sidebar-link reco-side-_5-命名路由" data-v-cb1513f6>5.命名路由</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_6-路由的params参数" class="sidebar-link reco-side-_6-路由的params参数" data-v-cb1513f6>6.路由的params参数</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_7-路由的props配置" class="sidebar-link reco-side-_7-路由的props配置" data-v-cb1513f6>7.路由的props配置</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_8-router-link-的replace属性" class="sidebar-link reco-side-_8-router-link-的replace属性" data-v-cb1513f6>8.`&lt;router-link&gt;`的replace属性</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_9-编程式路由导航" class="sidebar-link reco-side-_9-编程式路由导航" data-v-cb1513f6>9.编程式路由导航</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_10-缓存路由组件" class="sidebar-link reco-side-_10-缓存路由组件" data-v-cb1513f6>10.缓存路由组件</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_11-两个新的生命周期钩子" class="sidebar-link reco-side-_11-两个新的生命周期钩子" data-v-cb1513f6>11.两个新的生命周期钩子</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_12-路由守卫" class="sidebar-link reco-side-_12-路由守卫" data-v-cb1513f6>12.路由守卫</a></li><li class="level-3" data-v-cb1513f6><a href="/ethanBlogs/notes/Vue2/GuiVue.html#_13-路由器的两种工作模式" class="sidebar-link reco-side-_13-路由器的两种工作模式" data-v-cb1513f6>13.路由器的两种工作模式</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div></div></div>
    <script src="/ethanBlogs/assets/js/app.41cfd4b1.js" defer></script><script src="/ethanBlogs/assets/js/3.30c6e821.js" defer></script><script src="/ethanBlogs/assets/js/1.ff0e4a20.js" defer></script><script src="/ethanBlogs/assets/js/21.73137a1f.js" defer></script>
  </body>
</html>
